package com.ase.反转链表;

/**
 * @author gyhstart
 * @create 2021/2/21 - 19:13
 * <p>
 * 定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
 * 示例:
 * <p>
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 **/
public class Solution {
    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        ListNode next;
        while (curr != null) {
          next = curr.next;
          curr.next = prev;
          prev = curr;
          curr = next;
        }
        return prev;
    }

    public static void getNodeList(ListNode node){
        while (node != null){
            System.out.print("node => " + node.data +"  ");
            node = node.next;
        }
    }

    static class ListNode {
        private int data;
        private ListNode next;

        public ListNode(int data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        getNodeList(node1);

        ListNode reversNode = reverseList(node1);
        System.out.println();
        getNodeList(reversNode);
    }
}
